/** @license torrent.js
 * Copyright (c) 2009 Fedor Indutny
 * License: GNU General Public License version 3 or later
 */
$(document).ready(function () {
  var documentlocationhref=document.location.href;
  var sk=false,ck=false,ak=false,mouse=false;
  var overlay=document.createElement('div');
  overlay.setAttribute('id','--tcd-overlay');
  var tcdddd=$(overlay);
  var allaas;
  var foundtracker;
  var st='text-align:center;background-color:#000000;color:#FFFFFF;opacity:0.8;position: absolute;left: 0px;right: 0px;display: none;-webkit-box-shadow: black 0px 0px 5px;border-bottom-left-radius: 7px 7px;border-bottom-right-radius: 7px 7px;border-top-left-radius: 7px 7px;border-top-right-radius: 7px 7px;width:250px;height:auto;font-size:10px;padding:15px;z-index:999';
  overlay.setAttribute('style',st);
  document.body.appendChild(overlay);
  //Get user trackers
  chrome.extension.onRequest.addListener(Response);
  chrome.extension.connect().postMessage('#^%connect');
  var timer;
  function uResponse(uText) {
    if (uText[0]=='---') {   
            overlay.innerHTML=uText[1];
            chrome.extension.onRequest.removeListener(uResponse);            
    }
    if ((uText[0]=='!!!')&&(uText[2]==documentlocationhref)) {
      foundtracker=false;
      if (uText[1]!='') {
      ttid = new RegExp(uText[1],'gim');
      if (item = allaas.filter(function (){return ttid.test(this.href);})) 
        if (item.length) {
          foundtracker=true;
          chrome.extension.connect().postMessage([item[0].href]);
        }
      }
    if (!foundtracker) {
    // Kickass support
      if ((/^http:\/\/www\.kickasstorrents\.com\/.*/gim).test(documentlocationhref)) {
        // Add icon if see there any torrent link 
        var result = document.evaluate(
            '//a[(@class="iverif") or (@class="idirect")]',
            document, null, 0, null);
  
        var item;
        if (item = result.iterateNext())  {
           chrome.extension.connect().postMessage(['$^$kickass',item.href,(documentlocationhref).replace(/\/[^\/]*$/gim,"")+'/']); 
        }
        // Memory Leaks???
        result=null;
        item=null;
        
      } else
    //Mininova.org support
      if ((/^http:\/\/www\.mininova\.org\/.*/gim).test(documentlocationhref)) {
       // Add icon if see there any torrent link 
  
        var item;
        if (item = $('a').filter(function (){return (/\/tor\/[0-9]+/i).test(this.href);})) {
          if (item[0]) {
            chrome.extension.connect().postMessage(['$^$mininova',item[0].href,(documentlocationhref).replace(/\/[^\/]*$/gim,"")+'/']); 
          }
        }
        // Memory Leaks???
        result=null;
        item=null;
        
      } else
    // Kinozal.tv support
      if ((/^http:\/\/kinozal\.tv\/.*/gim).test(documentlocationhref)) {
       // Add icon if see there any torrent link 
        var result = document.evaluate(
            '//a[contains(@href,"/details.php?id=")]',
            document, null, 0, null);
        var item;
        if (item = result.iterateNext())  {
           chrome.extension.connect().postMessage(['$^$kinozal',item.href,(documentlocationhref).replace(/\/[^\/]*$/gim,"")+'/']); 
        }
        // Memory Leaks???
        result=null;
        item=null;
        
      } else {
        // Other by mime-type/size
        var othertest=(/\.torrent(\?[^\.\/]*)?$/gi);
        var othertest2=(/download\.php(\?[^\.\/]*)?$/gi);
        item = allaas.filter(function (){             
          return othertest.test(this.href)||othertest2.test(this.href);
        });
        var jj=0;
        if ((item)&&(item.length))
        for (x in item) {
          jj++;
          if (jj>3) break;
          if (item[x].href)
            chrome.extension.connect().postMessage(['$^$other',item[x].href,(documentlocationhref).replace(/\/[^\/]*$/gim,"")+'/']); 
        }
        result=null;
        item=null;
      }  
    }  
    }
  };
  function Response(uTrackers) {
      allaas=$("a");
      allaas.hover(function(e) {
      if ((e.ctrlKey==ck)&&(e.altKey==ak)&&(e.shiftKey==sk)) {
        if (mouse==1) {
          var ethref;
          aaa=e.target;
          while ((aaa.parentNode)&&(aaa.nodeName!='A'))
          aaa=aaa.parentNode;
          ethref=aaa.href;
          chrome.extension.onRequest.addListener(uResponse);
          var x=e.clientX;
          var y=e.clientY;
    
          if (timer) clearTimeout(timer);
   
          timer=setTimeout(function () {
            chrome.extension.connect().postMessage(new Array('^^^',ethref));
            var tx=document.body.scrollLeft+x;
            if (tx+280>document.body.clientWidth) tx-=(tx+280-document.body.clientWidth);
            overlay.style.left=tx+'px';
 
            overlay.style.top=document.body.scrollTop+y+'px';
            overlay.innerHTML='<u>Loading...</u>';
            tcdddd.fadeIn(500);
          },800);
        }
      }
      
      if (tcdddd.display!='none')
        tcdddd.fadeOut('fast');
    }); 

    chrome.extension.onRequest.removeListener(Response);
  
    ck=(uTrackers[0].ck);
    sk=uTrackers[0].sk;
    ak=uTrackers[0].ak;
    mouse=uTrackers[0].mouse;

      chrome.extension.onRequest.addListener(uResponse);
      chrome.extension.connect().postMessage(new Array('!!!',documentlocationhref));
  }
}
);